package com.javasm.service.impl;

import com.javasm.dao.impl.MenuDaoImpl;
import com.javasm.entity.AdminMenu;
import com.javasm.entity.AdminUser;
import com.javasm.service.MenuService;

import javax.servlet.annotation.WebServlet;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author WangJie
 * @version 1.0
 * @Description:
 * @date: 2020/7/15 19:57
 * @since JDK 1.8
 */

public class MenuServiceImpl implements MenuService {
    /**
     * 通过每个用户的权限列表查询该用户的对应权限
     *
     * @param menuId 权限列表
     * @return
     */
    @Override
    public List<AdminMenu> selectList(String menuId) {
        System.out.println(menuId);
        if (menuId == null || "".equals(menuId)) { // 非空校验
            return null;
        }
        List<AdminMenu> adminMenus = new MenuDaoImpl().selectList(menuId);
        List<AdminMenu> menus = new ArrayList<>();
        for (AdminMenu adminMenu : adminMenus) {
            if (adminMenu.getFid() == 0) {
                menus.add(adminMenu);
            }
        }
        for (AdminMenu menu : menus) {
            for (AdminMenu adminMenu : adminMenus) {
                if (adminMenu.getFid() != 0 && adminMenu.getFid() == menu.getPid()) {
                    menu.getMenuList().add(adminMenu);
                }
            }
        }
        return menus;
    }

    /**
     * 查询菜单首选项信息
     *
     * @return
     */
    @Override
    public List<AdminMenu> selectMenuHead() {
        List<AdminMenu> adminMenuList = new MenuDaoImpl().selectMenuHead();
        hasChildCharge(adminMenuList);
        return adminMenuList;
    }

    /**
     * 通过父id查询子菜单信息
     *
     * @param pid
     * @return
     */
    @Override
    public List<AdminMenu> selectMenuByPid(Integer pid) {
        List<AdminMenu> adminMenuList = new MenuDaoImpl().selectMenuByPid(pid);
        hasChildCharge(adminMenuList);
        return adminMenuList;
    }


    // 递归判断是否含有子节点
    private void hasChildCharge(List<AdminMenu> adminMenuList) {
        for (AdminMenu adminMenu : adminMenuList) {
            List<AdminMenu> childMenu = new MenuDaoImpl().selectMenuByPid(adminMenu.getPid());
            if (childMenu.size() == 0) {
                adminMenu.setHasChild(false);
            } else {
                adminMenu.setHasChild(true);
                hasChildCharge(childMenu);
            }
        }
    }

    /**
     * 查询所有菜单信息，用于权限管理的树形图展示
     *
     * @return
     */
    @Override
    public List<Map<String, Object>> selectAllMenu() {
        List<Map<String, Object>> menuKey = new MenuDaoImpl().selectMenuKey();
        addChild(menuKey);
        return menuKey;
    }

    // 递归遍历子菜单
    private void addChild(List<Map<String, Object>> menuKey) {
        for (Map<String, Object> map : menuKey) {
            List<Map<String, Object>> childMenu = new MenuDaoImpl().selectChildMenu((int) map.get("id"));
            if (childMenu.size() == 0) {
                continue;
            } else {
                map.put("children", childMenu);
                addChild(childMenu);
            }
        }

    }


}
